{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/usr/local/anaconda3/envs/aes/lib/python3.6/site-packages/ipykernel_launcher.py:10: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  # Remove the CWD from sys.path while we load stuff.\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "df = pd.read_csv('selected-ann.csv')\n",
    "\n",
    "stop_words = open('stopwords.txt', 'r', encoding='utf8').read().split('\\n')\n",
    "for index, row in df.iterrows():\n",
    "    content = row['content']\n",
    "    for stop_word in stop_words:\n",
    "        content = content.replace(stop_word, '')\n",
    "    df.iloc[index]['content'] = content\n",
    "\n",
    "from gensim.models import KeyedVectors\n",
    "\n",
    "word_vectors = KeyedVectors.load_word2vec_format('sgns.baidubaike.bigram-char', binary=False)  # 确保路径是正确的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Building prefix dict from the default dictionary ...\n",
      "Loading model from cache /tmp/jieba.cache\n",
      "Loading model cost 0.486 seconds.\n",
      "Prefix dict has been built successfully.\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import jieba\n",
    "\n",
    "MAX_SEQ_LEN = 500  # 假设我们设置的最大序列长度为100\n",
    "VECTOR_DIM = 300  # 假设词向量的维度为300\n",
    "\n",
    "def text_to_fixed_sequence(text, model, max_seq_len, vector_dim):\n",
    "    words = list(jieba.cut(text))\n",
    "    vectors = []\n",
    "    \n",
    "    for word in words:\n",
    "        try:\n",
    "            vec = model[word]\n",
    "            vectors.append(vec)\n",
    "        except KeyError:  # 如果词不在模型的词汇表中\n",
    "            pass\n",
    "\n",
    "    # 如果文本的长度小于max_seq_len，那么进行填充\n",
    "    while len(vectors) < max_seq_len:\n",
    "        vectors.append(np.zeros(vector_dim))\n",
    "\n",
    "    # 如果文本的长度大于max_seq_len，那么进行裁剪\n",
    "    return np.array(vectors[:max_seq_len])\n",
    "\n",
    "# 将每个文本转化为等长的向量序列\n",
    "df['vector_sequence'] = df['content'].apply(lambda x: text_to_fixed_sequence(x, word_vectors, MAX_SEQ_LEN, VECTOR_DIM))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Train on 480 samples\n",
      "Epoch 1/100\n",
      "480/480 [==============================] - 15s 32ms/sample - loss: 3.4756\n",
      "Epoch 2/100\n",
      "480/480 [==============================] - 11s 23ms/sample - loss: 2.5479\n",
      "Epoch 3/100\n",
      "480/480 [==============================] - 11s 23ms/sample - loss: 2.3909\n",
      "Epoch 4/100\n",
      "480/480 [==============================] - 11s 23ms/sample - loss: 2.2774\n",
      "Epoch 5/100\n",
      "480/480 [==============================] - 11s 23ms/sample - loss: 2.3118\n",
      "Epoch 6/100\n",
      "480/480 [==============================] - 11s 23ms/sample - loss: 2.2680\n",
      "Epoch 7/100\n",
      "480/480 [==============================] - 11s 23ms/sample - loss: 1.9937\n",
      "Epoch 8/100\n",
      "480/480 [==============================] - 11s 23ms/sample - loss: 1.9342\n",
      "Epoch 9/100\n",
      "480/480 [==============================] - 11s 23ms/sample - loss: 1.9069\n",
      "Epoch 10/100\n",
      "480/480 [==============================] - 11s 23ms/sample - loss: 1.5816\n",
      "Epoch 11/100\n",
      "480/480 [==============================] - 11s 23ms/sample - loss: 1.4149\n",
      "Epoch 12/100\n",
      "480/480 [==============================] - 11s 23ms/sample - loss: 1.6772\n",
      "Epoch 13/100\n",
      "480/480 [==============================] - 11s 23ms/sample - loss: 1.4124\n",
      "Epoch 14/100\n",
      "480/480 [==============================] - 11s 23ms/sample - loss: 1.2579\n",
      "Epoch 15/100\n",
      "480/480 [==============================] - 11s 23ms/sample - loss: 1.1782\n",
      "Epoch 16/100\n",
      "480/480 [==============================] - 11s 23ms/sample - loss: 1.0673\n",
      "Epoch 17/100\n",
      "480/480 [==============================] - 11s 23ms/sample - loss: 1.1537\n",
      "Epoch 18/100\n",
      "480/480 [==============================] - 11s 23ms/sample - loss: 0.9070\n",
      "Epoch 19/100\n",
      "480/480 [==============================] - 11s 23ms/sample - loss: 0.8016\n",
      "Epoch 20/100\n",
      "480/480 [==============================] - 11s 23ms/sample - loss: 0.7873\n",
      "Epoch 21/100\n",
      "480/480 [==============================] - 11s 23ms/sample - loss: 0.7447\n",
      "Epoch 22/100\n",
      "480/480 [==============================] - 11s 23ms/sample - loss: 0.5580\n",
      "Epoch 23/100\n",
      "480/480 [==============================] - 11s 23ms/sample - loss: 0.4765\n",
      "Epoch 24/100\n",
      "480/480 [==============================] - 11s 23ms/sample - loss: 0.5043\n",
      "Epoch 25/100\n",
      "480/480 [==============================] - 11s 23ms/sample - loss: 0.3389\n",
      "Epoch 26/100\n",
      "480/480 [==============================] - 11s 23ms/sample - loss: 0.3176\n",
      "Epoch 27/100\n",
      "480/480 [==============================] - 11s 23ms/sample - loss: 0.3216\n",
      "Epoch 28/100\n",
      "480/480 [==============================] - 11s 23ms/sample - loss: 0.2635\n",
      "Epoch 29/100\n",
      "480/480 [==============================] - 11s 23ms/sample - loss: 0.2606\n",
      "Epoch 30/100\n",
      "480/480 [==============================] - 11s 23ms/sample - loss: 0.2393\n",
      "Epoch 31/100\n",
      "480/480 [==============================] - 11s 23ms/sample - loss: 0.2077\n",
      "Epoch 32/100\n",
      "480/480 [==============================] - 11s 23ms/sample - loss: 0.1904\n",
      "Epoch 33/100\n",
      "480/480 [==============================] - 11s 23ms/sample - loss: 0.2208\n",
      "Epoch 34/100\n",
      "480/480 [==============================] - 11s 23ms/sample - loss: 0.2075\n",
      "Epoch 35/100\n",
      "480/480 [==============================] - 11s 23ms/sample - loss: 0.1807\n",
      "Epoch 36/100\n",
      "480/480 [==============================] - 11s 23ms/sample - loss: 0.1720\n",
      "Epoch 37/100\n",
      "480/480 [==============================] - 11s 23ms/sample - loss: 0.1720\n",
      "Epoch 38/100\n",
      "480/480 [==============================] - 11s 23ms/sample - loss: 0.1583\n",
      "Epoch 39/100\n",
      "480/480 [==============================] - 11s 23ms/sample - loss: 0.1621\n",
      "Epoch 40/100\n",
      "480/480 [==============================] - 11s 23ms/sample - loss: 0.1397\n",
      "Epoch 41/100\n",
      "480/480 [==============================] - 11s 23ms/sample - loss: 0.1487\n",
      "Epoch 42/100\n",
      "480/480 [==============================] - 11s 23ms/sample - loss: 0.1368\n",
      "Epoch 43/100\n",
      "480/480 [==============================] - 11s 22ms/sample - loss: 0.1478\n",
      "Epoch 44/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.1330\n",
      "Epoch 45/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.1131\n",
      "Epoch 46/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.1282\n",
      "Epoch 47/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.1462\n",
      "Epoch 48/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.1371\n",
      "Epoch 49/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.1209\n",
      "Epoch 50/100\n",
      "448/480 [===========================>..] - ETA: 0s - loss: 0.1458Restoring model weights from the end of the best epoch.\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.1516\n",
      "Epoch 00050: early stopping\n",
      "Train on 480 samples\n",
      "Epoch 1/100\n",
      "480/480 [==============================] - 14s 29ms/sample - loss: 3.4788\n",
      "Epoch 2/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 2.6695\n",
      "Epoch 3/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 2.3643\n",
      "Epoch 4/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 2.1421\n",
      "Epoch 5/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 2.0585\n",
      "Epoch 6/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 1.9028\n",
      "Epoch 7/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 1.6274\n",
      "Epoch 8/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 1.4519\n",
      "Epoch 9/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 1.7634\n",
      "Epoch 10/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 1.7108\n",
      "Epoch 11/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 1.5703\n",
      "Epoch 12/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 1.4543\n",
      "Epoch 13/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 1.2155\n",
      "Epoch 14/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 1.0806\n",
      "Epoch 15/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 1.0919\n",
      "Epoch 16/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 1.0929\n",
      "Epoch 17/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.9200\n",
      "Epoch 18/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.8469\n",
      "Epoch 19/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.8362\n",
      "Epoch 20/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.7874\n",
      "Epoch 21/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.7764\n",
      "Epoch 22/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.6962\n",
      "Epoch 23/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.8173\n",
      "Epoch 24/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.6378\n",
      "Epoch 25/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.5723\n",
      "Epoch 26/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.4598\n",
      "Epoch 27/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.4870\n",
      "Epoch 28/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.4477\n",
      "Epoch 29/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.3710\n",
      "Epoch 30/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.3718\n",
      "Epoch 31/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.3528\n",
      "Epoch 32/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.3280\n",
      "Epoch 33/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.3557\n",
      "Epoch 34/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.3247\n",
      "Epoch 35/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.2806\n",
      "Epoch 36/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.2685\n",
      "Epoch 37/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.2753\n",
      "Epoch 38/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.2971\n",
      "Epoch 39/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.3238\n",
      "Epoch 40/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.2542\n",
      "Epoch 41/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.2124\n",
      "Epoch 42/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.2200\n",
      "Epoch 43/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.1906\n",
      "Epoch 44/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.2160\n",
      "Epoch 45/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.1904\n",
      "Epoch 46/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.1986\n",
      "Epoch 47/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.1919\n",
      "Epoch 48/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.2729\n",
      "Epoch 49/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.2957\n",
      "Epoch 50/100\n",
      "448/480 [===========================>..] - ETA: 0s - loss: 0.1995Restoring model weights from the end of the best epoch.\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.1994\n",
      "Epoch 00050: early stopping\n",
      "Train on 480 samples\n",
      "Epoch 1/100\n",
      "480/480 [==============================] - 14s 29ms/sample - loss: 3.4696\n",
      "Epoch 2/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 2.4905\n",
      "Epoch 3/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 2.5376\n",
      "Epoch 4/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 2.3671\n",
      "Epoch 5/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 2.1306\n",
      "Epoch 6/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 2.0192\n",
      "Epoch 7/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 1.8475\n",
      "Epoch 8/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 1.8953\n",
      "Epoch 9/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 1.5856\n",
      "Epoch 10/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 1.2985\n",
      "Epoch 11/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 1.3534\n",
      "Epoch 12/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 1.1925\n",
      "Epoch 13/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 1.4522\n",
      "Epoch 14/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 1.0956\n",
      "Epoch 15/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.8987\n",
      "Epoch 16/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.8590\n",
      "Epoch 17/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 1.0624\n",
      "Epoch 18/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.9367\n",
      "Epoch 19/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.6820\n",
      "Epoch 20/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.6055\n",
      "Epoch 21/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.5733\n",
      "Epoch 22/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.5171\n",
      "Epoch 23/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.4683\n",
      "Epoch 24/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.3975\n",
      "Epoch 25/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.3587\n",
      "Epoch 26/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.3035\n",
      "Epoch 27/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.2817\n",
      "Epoch 28/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.2769\n",
      "Epoch 29/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.3277\n",
      "Epoch 30/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.2906\n",
      "Epoch 31/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.2609\n",
      "Epoch 32/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.2705\n",
      "Epoch 33/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.2871\n",
      "Epoch 34/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.2104\n",
      "Epoch 35/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.1990\n",
      "Epoch 36/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.2375\n",
      "Epoch 37/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.2430\n",
      "Epoch 38/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.2418\n",
      "Epoch 39/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.2194\n",
      "Epoch 40/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.1898\n",
      "Epoch 41/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.1660\n",
      "Epoch 42/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.1896\n",
      "Epoch 43/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.1754\n",
      "Epoch 44/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.1551\n",
      "Epoch 45/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.1699\n",
      "Epoch 46/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.1608\n",
      "Epoch 47/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.1692\n",
      "Epoch 48/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.1656\n",
      "Epoch 49/100\n",
      "448/480 [===========================>..] - ETA: 0s - loss: 0.1979Restoring model weights from the end of the best epoch.\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.1904\n",
      "Epoch 00049: early stopping\n",
      "Train on 480 samples\n",
      "Epoch 1/100\n",
      "480/480 [==============================] - 14s 29ms/sample - loss: 3.5881\n",
      "Epoch 2/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 2.6849\n",
      "Epoch 3/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 2.3630\n",
      "Epoch 4/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 2.2851\n",
      "Epoch 5/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 2.2234\n",
      "Epoch 6/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 2.0805\n",
      "Epoch 7/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 2.1758\n",
      "Epoch 8/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 1.7862\n",
      "Epoch 9/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 1.8448\n",
      "Epoch 10/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 1.5103\n",
      "Epoch 11/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 1.5644\n",
      "Epoch 12/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 1.4503\n",
      "Epoch 13/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 1.3400\n",
      "Epoch 14/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 1.1325\n",
      "Epoch 15/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 1.0114\n",
      "Epoch 16/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.9752\n",
      "Epoch 17/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.9091\n",
      "Epoch 18/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.7661\n",
      "Epoch 19/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.8242\n",
      "Epoch 20/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.7479\n",
      "Epoch 21/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.7491\n",
      "Epoch 22/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.6109\n",
      "Epoch 23/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.4881\n",
      "Epoch 24/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.5148\n",
      "Epoch 25/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.4874\n",
      "Epoch 26/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.4336\n",
      "Epoch 27/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.4407\n",
      "Epoch 28/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.4253\n",
      "Epoch 29/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.4156\n",
      "Epoch 30/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.3113\n",
      "Epoch 31/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.2746\n",
      "Epoch 32/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.2683\n",
      "Epoch 33/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.2389\n",
      "Epoch 34/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.2314\n",
      "Epoch 35/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.2344\n",
      "Epoch 36/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.2166\n",
      "Epoch 37/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.2306\n",
      "Epoch 38/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.2023\n",
      "Epoch 39/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.2143\n",
      "Epoch 40/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.2005\n",
      "Epoch 41/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.1796\n",
      "Epoch 42/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.1746\n",
      "Epoch 43/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.1687\n",
      "Epoch 44/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.1984\n",
      "Epoch 45/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.1974\n",
      "Epoch 46/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.1965\n",
      "Epoch 47/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.1772\n",
      "Epoch 48/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.1569\n",
      "Epoch 49/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.1688\n",
      "Epoch 50/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.1824\n",
      "Epoch 51/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.1798\n",
      "Epoch 52/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.1629\n",
      "Epoch 53/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.1545\n",
      "Epoch 54/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.1674\n",
      "Epoch 55/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.1463\n",
      "Epoch 56/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.1387\n",
      "Epoch 57/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.1617\n",
      "Epoch 58/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.1471\n",
      "Epoch 59/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.1537\n",
      "Epoch 60/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.1981\n",
      "Epoch 61/100\n",
      "448/480 [===========================>..] - ETA: 0s - loss: 0.2010Restoring model weights from the end of the best epoch.\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.2010\n",
      "Epoch 00061: early stopping\n",
      "Train on 480 samples\n",
      "Epoch 1/100\n",
      "480/480 [==============================] - 14s 29ms/sample - loss: 3.3219\n",
      "Epoch 2/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 2.6862\n",
      "Epoch 3/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 2.3263\n",
      "Epoch 4/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 2.1662\n",
      "Epoch 5/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 1.9886\n",
      "Epoch 6/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 2.1692\n",
      "Epoch 7/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 1.7862\n",
      "Epoch 8/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 1.5861\n",
      "Epoch 9/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 1.5507\n",
      "Epoch 10/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 1.5015\n",
      "Epoch 11/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 1.2669\n",
      "Epoch 12/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 1.1817\n",
      "Epoch 13/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 1.0881\n",
      "Epoch 14/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 1.2296\n",
      "Epoch 15/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.9754\n",
      "Epoch 16/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.8124\n",
      "Epoch 17/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.7330\n",
      "Epoch 18/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.6630\n",
      "Epoch 19/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.5768\n",
      "Epoch 20/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.5624\n",
      "Epoch 21/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.5581\n",
      "Epoch 22/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.5052\n",
      "Epoch 23/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.5500\n",
      "Epoch 24/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.4906\n",
      "Epoch 25/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.3907\n",
      "Epoch 26/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.3080\n",
      "Epoch 27/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.3120\n",
      "Epoch 28/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.2790\n",
      "Epoch 29/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.3084\n",
      "Epoch 30/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.2716\n",
      "Epoch 31/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.2675\n",
      "Epoch 32/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.2543\n",
      "Epoch 33/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.2373\n",
      "Epoch 34/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.2249\n",
      "Epoch 35/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.2457\n",
      "Epoch 36/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.2052\n",
      "Epoch 37/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.1730\n",
      "Epoch 38/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.1889\n",
      "Epoch 39/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.2134\n",
      "Epoch 40/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.1929\n",
      "Epoch 41/100\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.2007\n",
      "Epoch 42/100\n",
      "448/480 [===========================>..] - ETA: 0s - loss: 0.2056Restoring model weights from the end of the best epoch.\n",
      "480/480 [==============================] - 10s 21ms/sample - loss: 0.2111\n",
      "Epoch 00042: early stopping\n",
      "Train on 480 samples\n",
      "Epoch 1/100\n",
      "480/480 [==============================] - 1s 1ms/sample - loss: 2.8824\n",
      "Epoch 2/100\n",
      "480/480 [==============================] - 0s 570us/sample - loss: 1.2655\n",
      "Epoch 3/100\n",
      "480/480 [==============================] - 0s 559us/sample - loss: 0.6390\n",
      "Epoch 4/100\n",
      "480/480 [==============================] - 0s 567us/sample - loss: 0.3664\n",
      "Epoch 5/100\n",
      "480/480 [==============================] - 0s 585us/sample - loss: 0.2304\n",
      "Epoch 6/100\n",
      "480/480 [==============================] - 0s 570us/sample - loss: 0.1559\n",
      "Epoch 7/100\n",
      "480/480 [==============================] - 0s 568us/sample - loss: 0.1243\n",
      "Epoch 8/100\n",
      "480/480 [==============================] - 0s 534us/sample - loss: 0.1395\n",
      "Epoch 9/100\n",
      "480/480 [==============================] - 0s 548us/sample - loss: 0.0981\n",
      "Epoch 10/100\n",
      "480/480 [==============================] - 0s 595us/sample - loss: 0.0905\n",
      "Epoch 11/100\n",
      "480/480 [==============================] - 0s 572us/sample - loss: 0.1489\n",
      "Epoch 12/100\n",
      "480/480 [==============================] - 0s 575us/sample - loss: 0.1541\n",
      "Epoch 13/100\n",
      "480/480 [==============================] - 0s 563us/sample - loss: 0.0906\n",
      "Epoch 14/100\n",
      "480/480 [==============================] - 0s 577us/sample - loss: 0.0983\n",
      "Epoch 15/100\n",
      "416/480 [=========================>....] - ETA: 0s - loss: 0.1035Restoring model weights from the end of the best epoch.\n",
      "480/480 [==============================] - 0s 579us/sample - loss: 0.0958\n",
      "Epoch 00015: early stopping\n",
      "Train on 480 samples\n",
      "Epoch 1/100\n",
      "480/480 [==============================] - 1s 2ms/sample - loss: 3.2137\n",
      "Epoch 2/100\n",
      "480/480 [==============================] - 0s 582us/sample - loss: 1.8529\n",
      "Epoch 3/100\n",
      "480/480 [==============================] - 0s 576us/sample - loss: 1.0342\n",
      "Epoch 4/100\n",
      "480/480 [==============================] - 0s 581us/sample - loss: 0.5964\n",
      "Epoch 5/100\n",
      "480/480 [==============================] - 0s 559us/sample - loss: 0.3795\n",
      "Epoch 6/100\n",
      "480/480 [==============================] - 0s 549us/sample - loss: 0.2351\n",
      "Epoch 7/100\n",
      "480/480 [==============================] - 0s 564us/sample - loss: 0.1835\n",
      "Epoch 8/100\n",
      "480/480 [==============================] - 0s 564us/sample - loss: 0.1599\n",
      "Epoch 9/100\n",
      "480/480 [==============================] - 0s 572us/sample - loss: 0.1597\n",
      "Epoch 10/100\n",
      "480/480 [==============================] - 0s 550us/sample - loss: 0.1325\n",
      "Epoch 11/100\n",
      "480/480 [==============================] - 0s 567us/sample - loss: 0.1066\n",
      "Epoch 12/100\n",
      "480/480 [==============================] - 0s 581us/sample - loss: 0.1031\n",
      "Epoch 13/100\n",
      "480/480 [==============================] - 0s 574us/sample - loss: 0.0866\n",
      "Epoch 14/100\n",
      "480/480 [==============================] - 0s 571us/sample - loss: 0.0821\n",
      "Epoch 15/100\n",
      "480/480 [==============================] - 0s 559us/sample - loss: 0.0787\n",
      "Epoch 16/100\n",
      "480/480 [==============================] - 0s 571us/sample - loss: 0.0834\n",
      "Epoch 17/100\n",
      "480/480 [==============================] - 0s 570us/sample - loss: 0.0749\n",
      "Epoch 18/100\n",
      "480/480 [==============================] - 0s 550us/sample - loss: 0.0684\n",
      "Epoch 19/100\n",
      "480/480 [==============================] - 0s 555us/sample - loss: 0.0607\n",
      "Epoch 20/100\n",
      "480/480 [==============================] - 0s 570us/sample - loss: 0.0706\n",
      "Epoch 21/100\n",
      "480/480 [==============================] - 0s 564us/sample - loss: 0.0847\n",
      "Epoch 22/100\n",
      "480/480 [==============================] - 0s 569us/sample - loss: 0.0739\n",
      "Epoch 23/100\n",
      "480/480 [==============================] - 0s 575us/sample - loss: 0.0702\n",
      "Epoch 24/100\n",
      "416/480 [=========================>....] - ETA: 0s - loss: 0.0707Restoring model weights from the end of the best epoch.\n",
      "480/480 [==============================] - 0s 573us/sample - loss: 0.0654\n",
      "Epoch 00024: early stopping\n",
      "Train on 480 samples\n",
      "Epoch 1/100\n",
      "480/480 [==============================] - 1s 1ms/sample - loss: 2.5613\n",
      "Epoch 2/100\n",
      "480/480 [==============================] - 0s 567us/sample - loss: 1.1479\n",
      "Epoch 3/100\n",
      "480/480 [==============================] - 0s 559us/sample - loss: 0.6185\n",
      "Epoch 4/100\n",
      "480/480 [==============================] - 0s 562us/sample - loss: 0.3631\n",
      "Epoch 5/100\n",
      "480/480 [==============================] - 0s 568us/sample - loss: 0.2165\n",
      "Epoch 6/100\n",
      "480/480 [==============================] - 0s 559us/sample - loss: 0.1631\n",
      "Epoch 7/100\n",
      "480/480 [==============================] - 0s 558us/sample - loss: 0.1240\n",
      "Epoch 8/100\n",
      "480/480 [==============================] - 0s 582us/sample - loss: 0.1189\n",
      "Epoch 9/100\n",
      "480/480 [==============================] - 0s 587us/sample - loss: 0.0971\n",
      "Epoch 10/100\n",
      "480/480 [==============================] - 0s 576us/sample - loss: 0.1098\n",
      "Epoch 11/100\n",
      "480/480 [==============================] - 0s 590us/sample - loss: 0.1056\n",
      "Epoch 12/100\n",
      "480/480 [==============================] - 0s 568us/sample - loss: 0.1180\n",
      "Epoch 13/100\n",
      "480/480 [==============================] - 0s 569us/sample - loss: 0.1070\n",
      "Epoch 14/100\n",
      "480/480 [==============================] - 0s 578us/sample - loss: 0.0831\n",
      "Epoch 15/100\n",
      "480/480 [==============================] - 0s 584us/sample - loss: 0.0771\n",
      "Epoch 16/100\n",
      "480/480 [==============================] - 0s 566us/sample - loss: 0.0681\n",
      "Epoch 17/100\n",
      "480/480 [==============================] - 0s 584us/sample - loss: 0.0667\n",
      "Epoch 18/100\n",
      "480/480 [==============================] - 0s 556us/sample - loss: 0.0710\n",
      "Epoch 19/100\n",
      "480/480 [==============================] - 0s 575us/sample - loss: 0.0588\n",
      "Epoch 20/100\n",
      "480/480 [==============================] - 0s 585us/sample - loss: 0.0650\n",
      "Epoch 21/100\n",
      "480/480 [==============================] - 0s 581us/sample - loss: 0.0470\n",
      "Epoch 22/100\n",
      "480/480 [==============================] - 0s 585us/sample - loss: 0.0525\n",
      "Epoch 23/100\n",
      "480/480 [==============================] - 0s 581us/sample - loss: 0.0460\n",
      "Epoch 24/100\n",
      "480/480 [==============================] - 0s 588us/sample - loss: 0.0389\n",
      "Epoch 25/100\n",
      "480/480 [==============================] - 0s 583us/sample - loss: 0.0540\n",
      "Epoch 26/100\n",
      "480/480 [==============================] - 0s 589us/sample - loss: 0.0774\n",
      "Epoch 27/100\n",
      "480/480 [==============================] - 0s 584us/sample - loss: 0.0711\n",
      "Epoch 28/100\n",
      "480/480 [==============================] - 0s 578us/sample - loss: 0.0517\n",
      "Epoch 29/100\n",
      "416/480 [=========================>....] - ETA: 0s - loss: 0.0417Restoring model weights from the end of the best epoch.\n",
      "480/480 [==============================] - 0s 567us/sample - loss: 0.0401\n",
      "Epoch 00029: early stopping\n",
      "Train on 480 samples\n",
      "Epoch 1/100\n",
      "480/480 [==============================] - 1s 1ms/sample - loss: 2.4293\n",
      "Epoch 2/100\n",
      "480/480 [==============================] - 0s 573us/sample - loss: 1.1847\n",
      "Epoch 3/100\n",
      "480/480 [==============================] - 0s 578us/sample - loss: 0.6274\n",
      "Epoch 4/100\n",
      "480/480 [==============================] - 0s 573us/sample - loss: 0.3997\n",
      "Epoch 5/100\n",
      "480/480 [==============================] - 0s 577us/sample - loss: 0.2457\n",
      "Epoch 6/100\n",
      "480/480 [==============================] - 0s 568us/sample - loss: 0.1868\n",
      "Epoch 7/100\n",
      "480/480 [==============================] - 0s 572us/sample - loss: 0.1423\n",
      "Epoch 8/100\n",
      "480/480 [==============================] - 0s 579us/sample - loss: 0.1240\n",
      "Epoch 9/100\n",
      "480/480 [==============================] - 0s 584us/sample - loss: 0.1253\n",
      "Epoch 10/100\n",
      "480/480 [==============================] - 0s 582us/sample - loss: 0.1362\n",
      "Epoch 11/100\n",
      "480/480 [==============================] - 0s 572us/sample - loss: 0.1309\n",
      "Epoch 12/100\n",
      "480/480 [==============================] - 0s 574us/sample - loss: 0.1093\n",
      "Epoch 13/100\n",
      "480/480 [==============================] - 0s 568us/sample - loss: 0.1227\n",
      "Epoch 14/100\n",
      "480/480 [==============================] - 0s 565us/sample - loss: 0.1419\n",
      "Epoch 15/100\n",
      "480/480 [==============================] - 0s 568us/sample - loss: 0.1335\n",
      "Epoch 16/100\n",
      "480/480 [==============================] - 0s 568us/sample - loss: 0.0930\n",
      "Epoch 17/100\n",
      "480/480 [==============================] - 0s 566us/sample - loss: 0.1208\n",
      "Epoch 18/100\n",
      "480/480 [==============================] - 0s 582us/sample - loss: 0.1237\n",
      "Epoch 19/100\n",
      "480/480 [==============================] - 0s 572us/sample - loss: 0.1284\n",
      "Epoch 20/100\n",
      "480/480 [==============================] - 0s 570us/sample - loss: 0.1069\n",
      "Epoch 21/100\n",
      "480/480 [==============================] - 0s 575us/sample - loss: 0.0916\n",
      "Epoch 22/100\n",
      "480/480 [==============================] - 0s 576us/sample - loss: 0.0713\n",
      "Epoch 23/100\n",
      "480/480 [==============================] - 0s 571us/sample - loss: 0.0687\n",
      "Epoch 24/100\n",
      "480/480 [==============================] - 0s 569us/sample - loss: 0.0989\n",
      "Epoch 25/100\n",
      "480/480 [==============================] - 0s 575us/sample - loss: 0.1620\n",
      "Epoch 26/100\n",
      "480/480 [==============================] - 0s 571us/sample - loss: 0.2563\n",
      "Epoch 27/100\n",
      "480/480 [==============================] - 0s 582us/sample - loss: 0.2020\n",
      "Epoch 28/100\n",
      "416/480 [=========================>....] - ETA: 0s - loss: 0.1160Restoring model weights from the end of the best epoch.\n",
      "480/480 [==============================] - 0s 570us/sample - loss: 0.1081\n",
      "Epoch 00028: early stopping\n",
      "Train on 480 samples\n",
      "Epoch 1/100\n",
      "480/480 [==============================] - 1s 1ms/sample - loss: 3.3142\n",
      "Epoch 2/100\n",
      "480/480 [==============================] - 0s 598us/sample - loss: 1.6308\n",
      "Epoch 3/100\n",
      "480/480 [==============================] - 0s 570us/sample - loss: 0.9209\n",
      "Epoch 4/100\n",
      "480/480 [==============================] - 0s 582us/sample - loss: 0.5622\n",
      "Epoch 5/100\n",
      "480/480 [==============================] - 0s 598us/sample - loss: 0.3708\n",
      "Epoch 6/100\n",
      "480/480 [==============================] - 0s 571us/sample - loss: 0.2486\n",
      "Epoch 7/100\n",
      "480/480 [==============================] - 0s 586us/sample - loss: 0.1877\n",
      "Epoch 8/100\n",
      "480/480 [==============================] - 0s 567us/sample - loss: 0.1442\n",
      "Epoch 9/100\n",
      "480/480 [==============================] - 0s 591us/sample - loss: 0.1323\n",
      "Epoch 10/100\n",
      "480/480 [==============================] - 0s 609us/sample - loss: 0.1114\n",
      "Epoch 11/100\n",
      "480/480 [==============================] - 0s 568us/sample - loss: 0.0930\n",
      "Epoch 12/100\n",
      "480/480 [==============================] - 0s 569us/sample - loss: 0.1189\n",
      "Epoch 13/100\n",
      "480/480 [==============================] - 0s 587us/sample - loss: 0.0911\n",
      "Epoch 14/100\n",
      "480/480 [==============================] - 0s 586us/sample - loss: 0.0811\n",
      "Epoch 15/100\n",
      "480/480 [==============================] - 0s 577us/sample - loss: 0.0857\n",
      "Epoch 16/100\n",
      "480/480 [==============================] - 0s 561us/sample - loss: 0.0753\n",
      "Epoch 17/100\n",
      "480/480 [==============================] - 0s 572us/sample - loss: 0.0687\n",
      "Epoch 18/100\n",
      "480/480 [==============================] - 0s 581us/sample - loss: 0.0673\n",
      "Epoch 19/100\n",
      "480/480 [==============================] - 0s 599us/sample - loss: 0.0687\n",
      "Epoch 20/100\n",
      "480/480 [==============================] - 0s 590us/sample - loss: 0.0715\n",
      "Epoch 21/100\n",
      "480/480 [==============================] - 0s 586us/sample - loss: 0.0611\n",
      "Epoch 22/100\n",
      "480/480 [==============================] - 0s 591us/sample - loss: 0.0519\n",
      "Epoch 23/100\n",
      "480/480 [==============================] - 0s 591us/sample - loss: 0.0564\n",
      "Epoch 24/100\n",
      "480/480 [==============================] - 0s 579us/sample - loss: 0.0596\n",
      "Epoch 25/100\n",
      "480/480 [==============================] - 0s 602us/sample - loss: 0.0602\n",
      "Epoch 26/100\n",
      "480/480 [==============================] - 0s 591us/sample - loss: 0.0834\n",
      "Epoch 27/100\n",
      "480/480 [==============================] - 0s 588us/sample - loss: 0.0438\n",
      "Epoch 28/100\n",
      "480/480 [==============================] - 0s 570us/sample - loss: 0.0642\n",
      "Epoch 29/100\n",
      "480/480 [==============================] - 0s 602us/sample - loss: 0.0780\n",
      "Epoch 30/100\n",
      "480/480 [==============================] - 0s 587us/sample - loss: 0.0574\n",
      "Epoch 31/100\n",
      "480/480 [==============================] - 0s 599us/sample - loss: 0.0569\n",
      "Epoch 32/100\n",
      "416/480 [=========================>....] - ETA: 0s - loss: 0.0736Restoring model weights from the end of the best epoch.\n",
      "480/480 [==============================] - 0s 581us/sample - loss: 0.0754\n",
      "Epoch 00032: early stopping\n",
      "Average QWK for LSTM: 0.5003182686187142\n",
      "Average QWK for CNN: 0.5152013147082991\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "from tensorflow.keras.models import Sequential\n",
    "from tensorflow.keras.layers import LSTM, Dense, Conv1D, GlobalMaxPooling1D, Embedding, Dropout, Bidirectional\n",
    "from tensorflow.keras.optimizers import Adam\n",
    "from sklearn.model_selection import KFold, StratifiedKFold\n",
    "from sklearn.metrics import cohen_kappa_score\n",
    "from tensorflow.keras.callbacks import EarlyStopping\n",
    "\n",
    "# 将vector_sequence转换为适当的形式\n",
    "X = np.stack(df['vector_sequence'].values)\n",
    "y = df['score'].values\n",
    "early_stopping = EarlyStopping(monitor='loss', patience=5, verbose=1, restore_best_weights=True)\n",
    "\n",
    "def get_lstm_model(input_shape):\n",
    "    model = Sequential()\n",
    "    model.add(Bidirectional(LSTM(256, return_sequences=True), input_shape=input_shape))\n",
    "    model.add(Dropout(0.5))\n",
    "    model.add(Bidirectional(LSTM(128)))\n",
    "    model.add(Dropout(0.5))\n",
    "    model.add(Dense(1, activation='linear'))\n",
    "    model.compile(loss='mean_squared_error', optimizer=Adam(learning_rate=0.001))\n",
    "    return model\n",
    "\n",
    "def get_cnn_model(input_shape):\n",
    "    model = Sequential()\n",
    "    model.add(Conv1D(128, 3, activation='relu', input_shape=input_shape))\n",
    "    model.add(GlobalMaxPooling1D())\n",
    "    model.add(Dense(1, activation='linear'))\n",
    "    model.compile(loss='mean_squared_error', optimizer=Adam(learning_rate=0.001))\n",
    "    return model\n",
    "\n",
    "skf = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)\n",
    "\n",
    "def evaluate_model(model_func, model_name):\n",
    "    trues = []\n",
    "    preds = []\n",
    "    for train_index, val_index in skf.split(X, y):\n",
    "        X_train, X_val = X[train_index], X[val_index]\n",
    "        y_train, y_val = y[train_index], y[val_index]\n",
    "\n",
    "        model = model_func(X_train[0].shape)\n",
    "        model.fit(X_train, y_train, epochs=100, batch_size=32, verbose=1, callbacks=[early_stopping])\n",
    "\n",
    "        predictions = model.predict(X_val).flatten()\n",
    "        # 将预测结果四舍五入\n",
    "        predictions = np.rint(predictions).astype(int)\n",
    "        trues = np.concatenate((trues, y_val))\n",
    "        preds = np.concatenate((preds, predictions))\n",
    "    # save model to file\n",
    "    model.save(model_name + '.h5')\n",
    "    return cohen_kappa_score(trues, preds, weights='quadratic')\n",
    "\n",
    "lstm_qwk = evaluate_model(get_lstm_model, \"lstm\")\n",
    "cnn_qwk = evaluate_model(get_cnn_model, \"cnn\")\n",
    "\n",
    "print(f\"Average QWK for LSTM: {lstm_qwk}\")\n",
    "print(f\"Average QWK for CNN: {cnn_qwk}\")\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "jupyter",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.15"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
